package com.zcy.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zcy.domain.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${server.port}") //springboot提供的一种语法，用于在java代码中获取 application.yml配置中的参数值
    private Integer port;
    @GetMapping("/{id}")
    //当访问该方法时，出现异常（抛异常或超时）就会触发 熔断方法
    @HystrixCommand(fallbackMethod = "fallback")
    public User getById(@PathVariable Long id){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>当前访问的服务端口号是：" + port);
        if (id % 2 == 0) {
            int i = 1 / 0;
        }
        User user = new User();
        user.setId(id);
        user.setUserName("zhangsan");
        user.setPwd("12369");
        user.setOtherName("张三");
        return user;
    }

    //当访问的方法出现异常时，会直接跳转执行当前方法
    private User fallback(Long id) {
        User user = new User();
        user.setId(id);
        user.setOtherName("当前请求失败，返回结果由断路器返回");
        return user;
    }
}
